package com.yggdrasil.graphics;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import com.yggdrasil.graphics.input.KeyHandler;

public class Screen extends JFrame implements Runnable {

	private static final long serialVersionUID = 1L;
	public static final int WIDTH = 1280, HEIGHT = 800;
	WorldView wv;
	CharView cv;

	public Screen(WorldView world, CharView cv, KeyHandler kh) {
		setResizable(false);
		setSize(WIDTH, HEIGHT);
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		setLayout(new BorderLayout());
		addKeyListener(kh);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
		this.wv = world;
		this.cv = cv;
	}
	
	@Override
	public void run() {
		while (true) {
			display();
		}
	}
	
	public void display() {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}
		Graphics g = bs.getDrawGraphics();
		g.dispose();
		bs.show();
		wv.display();
	}
}
